<template>
  <div style="padding: 10px">
    <div style="margin:10px">
      <el-button type="primary" @click="add" size="large">新增</el-button>
      <el-button >导入</el-button>
      <el-button >导出</el-button>
    </div>
    <div style="margin:10px 0">
      <el-input v-model="search" placeholder="请输入您要查找的人的姓名" style="width:30%"/>
      <el-button type="primary" style="margin-left:10px" @click="load">查询</el-button>
    </div>
      <el-table :data="tableData" stripe border style="width: 100%" clearable>
        <el-table-column prop="id" label="编号" sortable auto width="100"/>
        <el-table-column prop="name" label="姓名" width="180"/>
        <el-table-column prop="unit" label="单位" width="100" />
        <el-table-column prop="uno" label="单位编号" width="100" />
        <el-table-column prop="sex" label="性别" width="100" />
        <el-table-column prop="address" label="户口地" sortable width="200" />
        <el-table-column prop="total" label="总消费" width="80" />
        <el-table-column prop="claimd" label="报销费用" width="100" />
        <el-table-column prop="actualpay" label="支付费用" width="100" />
        <el-table-column prop="annual" label="年度费用" width="100" />
        <el-table-column label="操作" width="150">
          <template #default="scope">
            <el-button size="small" @click="handleEdit(scope.row)">编辑</el-button>
            <el-popconfirm title="确定删除吗?" @confirm="handleDelete(scope.row.id)">
              <template #reference>
            <el-button size="small" type="danger">删除</el-button>
          </template>
            </el-popconfirm>
          </template>
        </el-table-column>


      </el-table>
    <div style="margin: 10px">
      <el-pagination
          :page-size="pageSize"
          :current-page="currentPage"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
      />
      <el-dialog  title="新增" v-model="dialogVisible" width="30%">
        <el-form :model="form" label-width="120px">

<!--          <el-form-item label="编号">-->
<!--            <el-input v-model="form.id" style="width: 80%"></el-input>-->
<!--          </el-form-item>-->
          <el-form-item label="姓名">
            <el-input v-model="form.name" style="width: 80%"></el-input>
          </el-form-item>
          <el-form-item label="单位">
            <el-input v-model="form.unit" style="width: 80%"></el-input>
          </el-form-item>
          <el-form-item label="单位编号">
            <el-input v-model="form.uno" style="width: 80%"></el-input>
          </el-form-item>
          <el-form-item label="性别">
            <el-radio v-model="form.sex" label="男">男</el-radio>
            <el-radio v-model="form.sex" label="女">女</el-radio>
          </el-form-item>
          <el-form-item label="户口地">
            <el-input v-model="form.address" style="width: 80%"></el-input>
          </el-form-item>
          <el-form-item label="总消费">
            <el-input v-model="form.total" style="width: 80%"></el-input>
          </el-form-item>
          <el-form-item label="报销费用">
            <el-input v-model="form.claimd" style="width: 80%"></el-input>
          </el-form-item>
            <el-form-item label="支付费用">
              <el-input v-model="form.actualpay" style="width: 80%"></el-input>
          </el-form-item>
          <el-form-item label="年度消费">
            <el-input v-model="form.annual" style="width: 80%"></el-input>
          </el-form-item>
        </el-form>
          <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="save">
            确定
          </el-button>
         </span>
        </template>
      </el-dialog
          >




  </div>

  </div>
</template>

<script>



import request from "../../utils/request"; //!!!!!!!

export default {
  name: 'People',
  components: {

  },
  data(){
    return{
      form:{},
      form1:{

      },
      form2:{

      },
      form4:{},
      dialogVisible:false,
      dialogVisible1:false,
      dialogVisible2:false,
      search:'',
      pageSize:10,
      currentPage:1,
      total:0,
      tableData: [],
      // rules: {
      //   学位课数量: [
      //     { required: true, message: '请输入学位课数量', trigger: 'blur' },
      //     { validator: this.validateNumberNotZero, trigger: 'blur' }
      //   ]
      // }

    }
  },
  created(){
    this.load()
  },
  methods:{
    load(){
      request.get("/people",{
        params: {
          pageNum: this.currentPage,
          pageSize: this.pageSize,
          search: this.search
        }
      }).then(res => {
        console.log(res)
        this.tableData = res.data.records
        this.total = res.data.total
      })
    },

    add(){
      this.dialogVisible=true;
      this.form= {}
      this.load()

    },
    save(){
      if(this.form.id) {
        request.put("/people", this.form).then(res => {
          console.log(res)
          if(res.code === '0'){
            this.$message({
              type:"success",
              message:"更新成功"
            })
          } else{
            this.$message({
              type:"error",
              message:res.msg
            })
          }
          this.load()
          this.dialogVisible = false //关闭弹窗
        })
      }else{ //新增
      request.post("/people",this.form).then(res =>{
        console.log(res)
        if(res.code === '0'){
          this.$message({
            type:"success",
            message:"新增成功"
          })
        } else{
          this.$message({
            type:"error",
            message:res.msg
          })
        }
        this.load()
        this.dialogVisible=false
      })}

    },
    handleEdit(row){
      this.form =JSON.parse(JSON.stringify(row))
      this.dialogVisible =true
    },
    handleDelete(id){
      console.log(id)
      request.delete('/people/'+ id).then(res =>{
        if(res.code === '0'){
          this.$message({
            type:"success",
            message:"删除成功"
          })
        } else{
          this.$message({
            type:"error",
            message:res.msg
          })
        }
        this.load()
      })
    },
    handleSizeChange(pageSize){
      this.pageSize = pageSize
      this.load()
    },
    handleCurrentChange(pageNum){
      this.currentPage = pageNum
      this.load()
    }

}
}
</script>
